package org.zs.miracle.service.route.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.zs.miracle.common.base.dto.BaseLongIdDTO;
import org.zs.miracle.common.base.dto.BaseManyLongIdDTO;
import org.zs.miracle.common.util.ModelMapperUtil;
import org.zs.miracle.entity.route.RouteEntity;
import org.zs.miracle.mapper.RouteMapper;
import org.zs.miracle.service.route.IRouteService;
import org.zs.miracle.service.route.model.dto.AddRouteDTO;
import org.zs.miracle.service.route.model.dto.EditRouteDTO;
import org.zs.miracle.service.route.model.dto.RouteQueryPageDTO;
import org.zs.miracle.service.route.model.vo.RouteVO;

/**
 * 骑行路线表Service服务接口实现层
 */
@Service
@AllArgsConstructor
public class IRouteServiceImpl implements IRouteService {
  
  @Resource
  RouteMapper routeMapper;
  
  @Override
  public Page<RouteVO> pageQueryList(RouteQueryPageDTO inputDTO) {
    
    // 构建查询条件
    LambdaQueryWrapper<RouteEntity> queryWrapper = Wrappers.lambdaQuery();
    
    // 执行查询用户
    
    return routeMapper.selectPage(inputDTO.toMybatisPageObject(), queryWrapper, RouteVO.class);
  }
  
  @Override public RouteVO getOne(BaseLongIdDTO inputDTO) {
    return routeMapper.selectById(inputDTO.getId(), RouteVO.class);
  }
  
  @Override
  public Boolean delRoute(BaseManyLongIdDTO inputDTO) {
    
    // 删除用户
    routeMapper.deleteByIds(inputDTO.getIds());
    return true;
  }
  
  @Override
  public Boolean addRoute(AddRouteDTO inputDTO) {
    
    // 将DTO转换为实体对象
    RouteEntity addEntity = ModelMapperUtil.map(inputDTO, RouteEntity.class);
    // 插入数据库
    routeMapper.insert(addEntity);
    
    return true;
  }
  
  @Override
  public Boolean editRoute(EditRouteDTO inputDTO) {
    
    // 更新的数据
    RouteEntity updateEntity = ModelMapperUtil.map(inputDTO, RouteEntity.class);
    
    // 执行更新
    routeMapper.updateById(updateEntity);
    
    return true;
  }
}